package com.example.myapplication.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

/**
 * Author:caiyoufei
 * Date:2016/7/26
 * Time:16:08
 */
public class ScreenUtils {
    /**
     * 获取屏幕宽度
     *
     * @param context 上下文
     * @return 屏幕宽度
     */
    public static int getScreenWidth(final Context context) {
        return context == null ? -1 : context.getResources().getDisplayMetrics().widthPixels;
        //WindowManager windowManager= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        //Point size=new Point();
        //windowManager.getDefaultDisplay().getSize(size);
        //return size.x;
    }

    /**
     * 获取屏幕高度
     *
     * @param context 上下文
     * @return 屏幕高度
     */
    public static int getScreenHeight(final Context context) {
        return context == null ? -1 : context.getResources().getDisplayMetrics().heightPixels;
        //WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        //DisplayMetrics dm = new DisplayMetrics();
        //windowManager.getDefaultDisplay().getMetrics(dm);
        //return dm.heightPixels;
    }

    /**
     * 获取手机状态栏高度
     *
     * @param context 上下文
     * @return 手机状态栏高度(获取失败返回-1)
     */
    public static int getStatusHeight(final Context context) {
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusHeight = context == null ? -1 : context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    /**
     * 获取手机底部虚拟按键高度
     *
     * @param context Activity实例
     * @return 底部虚拟按键高度
     */
    public static int getNavigationBarHeight(Activity context) {
        if (context != null && checkDeviceHasNavigationBar(context)) {
            Resources resources = context.getResources();
            int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
            int height = resources.getDimensionPixelSize(resourceId);
            return height;
        } else {
            return 0;
        }
    }

    /**
     * 判断是否有虚拟按键
     *
     * @param context Activity实例
     * @return 是否有虚拟按键
     */
    private static boolean checkDeviceHasNavigationBar(Activity context) {
        if (context == null) {
            return false;
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return false;
        } else {
            WindowManager windowManager = context.getWindowManager();
            Display d = windowManager.getDefaultDisplay();
            DisplayMetrics realDisplayMetrics = new DisplayMetrics();
            d.getRealMetrics(realDisplayMetrics);
            int realHeight = realDisplayMetrics.heightPixels;
            int realWidth = realDisplayMetrics.widthPixels;
            DisplayMetrics displayMetrics = new DisplayMetrics();
            d.getMetrics(displayMetrics);
            int displayHeight = displayMetrics.heightPixels;
            int displayWidth = displayMetrics.widthPixels;
            return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
        }
    }
}
